package com.eistert.java.datastructure._04_LinkedList;

import com.eistert.java.datastructure._02_Stack._02_Stack;


/**
 * @Description: Implement LinkedList with Stack
 * @Author: ai
 * @create: 2023-04-04 18:55
 */
public class _04_LinkedListStack<E> implements _02_Stack<E> {
    private _04_LinkedList<E> list;

    private _04_LinkedListStack() {
        list = new _04_LinkedList();
    }

    @Override
    public int getSize() {
        return list.getSize();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void push(E e) {
        list.addFirst(e);
    }

    @Override
    public E pop() {
        return list.removeFirst();
    }

    @Override
    public E peek() {
        return list.getFirst();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: top ");
        res.append(list);
        return res.toString();
    }

    public static void main(String[] args) {

        _04_LinkedListStack<Integer> stack = new _04_LinkedListStack<>();

        for (int i = 0; i < 5; i++) {
            stack.push(i);
            System.out.println(stack);
        }

        stack.pop();
        System.out.println(stack);
    }
}
